﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text;
using System.Drawing;

namespace FCNS.KeepMoney.UI
{
    public partial class Calc : Control
    {
        public Calc()
        {
            InitializeComponent();
        }

        Bitmap offBmp = null;
        int fontSize = 0;

        private void RePaint()
        {
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
                return;

            if ((offBmp == null) || (offBmp.Height != this.ClientRectangle.Height) || (offBmp.Width != this.ClientRectangle.Width))
            {
                if (offBmp != null)
                    offBmp.Dispose();

                offBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
            }

           
            Graphics g = PaintBackGround();
            PaintCalc(g);
            g.Dispose();
            Paint2();
        }

        private void Paint2()
        {
            if ((offBmp == null) || (offBmp.Height != this.ClientRectangle.Height) || (offBmp.Width != this.ClientRectangle.Width))
            {
                RePaint();
                return;
            }
            if ((this.ClientRectangle.Width < 1) || (this.ClientRectangle.Height < 1))
                return;

            if (!this.IsDisposed)//取消这里会导致控件无法刷新,不加验证在切换模式的时候会报错
            {
                Graphics drawingSurface = Graphics.FromHwnd(this.Handle);
                drawingSurface.DrawImage(offBmp, 0, 0);
                drawingSurface.Dispose();
            }
        }

        private Graphics PaintBackGround()
        {
            Graphics g = Graphics.FromImage(offBmp);
            g.Clear(Color.White);
            g.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);
            return g;
        }

        private void PaintCalc(Graphics g)
        {
            //if (calcResult.Length == 0)
            //    calcResult.Append(0);

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Far;
            Font f = new System.Drawing.Font("宋体",fontSize );

            g.DrawString(calcString.ToString(), f, Brushes.Black,
                new RectangleF(0, 2, this.Width - 3, f.Size+2), sf);

            f = new System.Drawing.Font("宋体", fontSize, FontStyle.Bold);
            g.DrawString(calcResult.ToString(), f, Brushes.Red,
                new RectangleF(0, f.Size+4, this.Width - 3, f.Size+2), sf);
            
            sf.Dispose();
            f.Dispose();
        }

        private void Calc_Paint(object sender, PaintEventArgs e)
        {
            RePaint();
        }

        private void Calc_SizeChanged(object sender, EventArgs e)
        {
            //fontSize =Decimal.ToInt32(this.Height / 2 - 5);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
            {
                calcResult.Append(e.KeyChar);
                RePaint();
            }
            else if (e.KeyChar == (char)Keys.Decimal)
            {
                if (calcResult.Length == 0||calcResult.ToString().Contains("."))
                    return;

                calcResult.Append(e.KeyChar);
            }
            else if (e.KeyChar == (char)Keys.Add || e.KeyChar == (char)Keys.Subtract || e.KeyChar == (char)Keys.Divide || e.KeyChar == (char)Keys.Multiply)
            {
                if (calcResult.Length == 0||calcResult[calcResult.Length-1]=='.')
                    return;

                calcString.Append(calcResult);
                calcString.Append(e.KeyChar);
                calcResult.Remove(0, calcResult.Length);
                RePaint();
            }
            else if (e.KeyChar == (char)Keys.Enter)
            {
                GetCalc();
                calcString.Remove(0, calcString.Length);
                RePaint();
            }
            else if (e.KeyChar == (char)Keys.Back)
            {
                if (calcString.Length == 0 || calcResult.Length == 0)
                    return;

                calcResult.Remove(calcResult.Length - 1, 1);
                RePaint();
            }

            base.OnKeyPress(e);
        }

        private void Calc_MouseClick(object sender, MouseEventArgs e)
        {
            this.Focus();
        }

        private void Calc_Leave(object sender, EventArgs e)
        {
            
        }
    }
}
